package com.ruoyi.pcj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.mashuai.utils.toolkit.core.msHandlebean.MsHandleDataUtils;
import com.ruoyi.basis.service.MsBaseServiceImpl;

import java.util.List;

import com.mashuai.utils.toolkit.core.msMp.MsDb;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.basis.utils.mp.MpConditionUtils;
import com.ruoyi.basis.utils.mp.MpPageUtils;
import com.ruoyi.pcj.domain.PcjTbArticle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.pcj.mapper.PcjTbSubjectMapper;
import com.ruoyi.pcj.domain.PcjTbSubject;
import com.ruoyi.pcj.service.IPcjTbSubjectService;

/**
 * 帖子话题Service业务层处理
 *
 * @author ms
 * @date 2024-03-09
 */
@Service
public class PcjTbSubjectServiceImpl extends MsBaseServiceImpl<PcjTbSubjectMapper, PcjTbSubject> implements IPcjTbSubjectService {

    @Override
    public <E extends IPage<PcjTbSubject>> E page(E page, Wrapper<PcjTbSubject> qw) {
        return baseMapper.selectPage(page, qw);
    }

    /**
     * 查询帖子话题列表
     *
     * @param params 聊天室-查询参数 {"pageNum":1,"pageSize":10}
     * @return 帖子话题
     */
    @Override
    public IPage<PcjTbSubject> selectPcjTbSubjectList(Map<String, Object> params) {
        //获取条件构造器
        QueryWrapper<PcjTbSubject> qw = MpConditionUtils.getQueryWrapper(params, PcjTbSubject.class);
        //分页查询
        Page<PcjTbSubject> page = this.page(MpPageUtils.getPage(params), qw);
        MsHandleDataUtils.handleTbOrDictValBeans(page.getRecords(), PcjTbSubject.class);
        return page;
    }

    @Override
    public List<String> selectPcjTbSubjectList(String name) {
        LambdaQueryWrapper<PcjTbSubject> qw = Wrappers.lambdaQuery();
        qw.likeRight(PcjTbSubject::getName, name);
        List<PcjTbSubject> tbSubjects = MsDb.list(qw, PcjTbSubject.class);
        return tbSubjects.stream().map(PcjTbSubject::getName).collect(Collectors.toList());
    }


    /**
     * 根据id查询帖子话题
     *
     * @param id 帖子话题主键
     * @return 帖子话题
     */
    @Override
    public PcjTbSubject selectPcjTbSubjectById(String id) {
        //首先获取主表信息
        PcjTbSubject pcjTbSubject = MsDb.getById(id, PcjTbSubject.class);
        MsHandleDataUtils.handleTbOrDictValBeans(pcjTbSubject, PcjTbSubject.class);
        return pcjTbSubject;
    }

    /**
     * 回填子表信息
     **/

    /**
     * 新增帖子话题
     *
     * @param pcjTbSubject 帖子话题
     * @return 结果
     */
    @Override
    public Boolean insertPcjTbSubject(PcjTbSubject pcjTbSubject) {
        //插入主表
        Boolean success = this.insertData(pcjTbSubject);
        return success;
    }

    /**
     * 修改帖子话题
     *
     * @param pcjTbSubject 帖子话题
     * @return 结果
     */
    @Override
    public Boolean updatePcjTbSubject(PcjTbSubject pcjTbSubject) {
        //修改主表
        Boolean success = this.updateData(pcjTbSubject);
        return success;
    }

    /**
     * 批量删除帖子话题
     *
     * @param ids 需要删除的帖子话题主键
     * @return 结果
     */
    @Override
    public Boolean deletePcjTbSubjectByIds(String[] ids) {

        //删除主表
        Boolean success = this.removeBatchByIds(Arrays.asList(ids));
        return success;
    }
    /**
     * 根据主表id，删除子表所有数据
     **/
    /**
     * 对外提供的设置默认字段值，id为空新增，不为空更新
     **/
    public void setEntityDefValAuto(PcjTbSubject pcjTbSubject) {
        this.setDefFieldVal(pcjTbSubject);
    }
}
